package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.entity.Type;
import pers.cj.pdos.entity.Unit;
import pers.cj.pdos.mapper.TypeMapper;
import pers.cj.pdos.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {
    @Autowired
    TypeMapper typeMapper;

    @Override
    public List<Type> list(String companyId) {
        return typeMapper.selectList(new QueryWrapper<Type>().eq("company_id",companyId));
    }

    @Override
    public List<Type> listPage(Integer page, Integer limit, String companyId) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);

        if (page !=null && limit != null) {
            IPage<Type> iPage = typeMapper.selectPage(new Page<Type>(page,limit), wrapper);
            return iPage.getRecords();
        } else {
            return typeMapper.selectList(wrapper);
        }
    }

    @Override
    public long count(String companyId) {
        return typeMapper.selectCount(new QueryWrapper<Type>().eq("company_id",companyId));
    }

    @Override
    public boolean checkName(String name, String companyId) {
        return typeMapper.selectCount(new QueryWrapper<Type>().eq("company_id",companyId).eq("name",name)) > 0;
    }

    @Override
    public void insert(Type type) {
        typeMapper.insert(type);
    }

    @Override
    public void remove(Type type) {
        typeMapper.deleteById(type);
    }

    @Override
    public void update(Type type) {
        typeMapper.updateById(type);
    }

    @Override
    public boolean checkById(String typeId, String companyId) {
        Type type = typeMapper.selectById(typeId);
        if (type == null) {
            return false;
        }

        if (type.getCompanyId().equals(companyId)){
            return true;
        } else {
            return false;
        }
    }
}
